package com.luwak.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.http.HttpUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenXc
 * @version V1.0
 * @Date 2018/4/7 下午12:10
 * @Description TODO(面部识别工具类)
 */
public class FaceMessageUtils {

    private final static String API_KEY = "GM6iCTiipd-5F-cyxQ1TWOooXxJMznUM";
    private final static String API_SECRET = "QLD2qwg-lOjQb0peNKnoIgPSNSwl31TT";
    private final static String REQ_URL = "https://api-cn.faceplusplus.com/facepp/v3/detect";

    public static String getFaceInfoByFile(File file) {


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("api_key", API_KEY);
        paramMap.put("api_secret", API_SECRET);
        paramMap.put("image_file", file);
        paramMap.put("return_attributes", "gender,age,smiling,eyestatus,emotion,ethnicity,beauty,skinstatus");

        String result = HttpUtil.post(REQ_URL, paramMap);
        JSONObject resultObject = JSONObject.parseObject(result);
        JSONArray facesArr = resultObject.getJSONArray("faces");

        JSONObject indexObject = (JSONObject) facesArr.get(0);
        JSONObject attrObject = indexObject.getJSONObject("attributes");



//        JSONObject attrObject = attrObject.getJSONObject("attributes");
        JSONObject smileObject = attrObject.getJSONObject("smile");
        JSONObject eyestatusObject = attrObject.getJSONObject("eyestatus");
        JSONObject beautyObject = attrObject.getJSONObject("beauty");
        JSONObject skinstatusObject = attrObject.getJSONObject("skinstatus");
        JSONObject ethnicity = attrObject.getJSONObject("ethnicity");
        JSONObject age = attrObject.getJSONObject("age");

        Map<String, Object> resultMap = new HashMap<>();

        //性别
        if (attrObject.getString("gender").equals("Male")) {
            resultMap.put("性别", "男性");
        } else {
            resultMap.put("性别", "女性");
        }

        //年龄
        resultMap.put("年龄", age.getString("value"));

        //笑容值
        resultMap.put("笑容值", smileObject.getDoubleValue("value"));

        //种族
        if (ethnicity.getString("value").equals("Asian")) {
            resultMap.put("种族", "亚洲人");
        } else if (attrObject.getString("ethnicity").equals("White")) {
            resultMap.put("种族", "白人");
        } else if (attrObject.getString("ethnicity").equals("Black")){
            resultMap.put("种族", "黑人");
        }

        //颜值
        resultMap.put("颜值(男性观点)", beautyObject.getDoubleValue("male_score"));
        resultMap.put("颜值(女性观点)", beautyObject.getDoubleValue("female_score"));

        //健康指数
        resultMap.put("健康指数", skinstatusObject.getDoubleValue("health"));

        return JSON.toJSONString(resultMap);
    }
}
